package com.painpointcloud.business.controller;

import com.painpointcloud.business.bean.UserMessage;
import com.painpointcloud.business.bean.UserModule;
import com.painpointcloud.business.bean.UserPicture;
import com.painpointcloud.business.service.UserMessageService;
import com.painpointcloud.business.service.UserModuleService;
import com.painpointcloud.business.service.UserPictureService;
import com.painpointcloud.common.util.core.Page;
import com.painpointcloud.common.util.core.ResUtil;
import com.painpointcloud.common.util.core.ServiceException;
import com.painpointcloud.common.util.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserModuleController extends BaseController {
    @Autowired
    private UserModuleService service;
    @Autowired
    private UserPictureService picservice;
    @Autowired
    private UserMessageService userMessageService;

    /**
     * 注册
     * @return
     */
    @RequestMapping("/sign.com")
    String sign() {
        return "business/app_signup";
    }
    /**
     * 登录菜单
     * @return
     */
   @RequestMapping("/login.com")
   String appLoginMapping() {
       return "business/app_login";
   }

    /**
     * 获取留言信息，跳转到留言页面
     * @return
     */
    @RequestMapping("/message.com")
    ModelAndView getMessageList(ModelMap model,@RequestParam String user_id) {
        model.addAttribute("userId",user_id);
        return new ModelAndView("/admin/user_msg_list");
    }
    /**
     * 可视化编辑
     * @param model
     * @param request
     * @param user_name
     * @return
     */
   @RequestMapping("/edit.com")
   public ModelAndView  editMapping(ModelMap model,HttpServletRequest request,@RequestParam(required = false) String user_name) {
       UserModule userModule=null;
       if(user_name==null){
           userModule=(UserModule)request.getSession().getAttribute("user_session");
       }else{
           if(request.getSession().getAttribute("user_session")==null){
               model.addAttribute("message","对不起，你没有访问权限,请先注册并且配置启用此模块后即可免费试用。");
               return new ModelAndView("error-400");
           }else{
               try {
                   UserModule param=new UserModule();
                   param.setUser_name(user_name);
                   userModule=service.selectUserModuleByName(param);
               } catch (Exception e) {
                   model.addAttribute("message","对不起，你没有访问权限,请先注册并且配置启用此模块后即可免费试用。");
                   return new ModelAndView("error-400");
               }
           }

       }

       if(userModule==null){
           model.addAttribute("message","对不起，你没有访问权限,请先注册并且配置启用此模块后即可免费试用。");
           return new ModelAndView("error-400");
       }else{
           model.addAttribute("userModule",userModule);
           return new ModelAndView("business/page_edit");
       }
   }
    /**
     * 用户二级域名登录
     * @param model
     * @param user_name
     * @param request_type
     * @return
     */
    @RequestMapping(value="/{user_name}.com")
    public ModelAndView show(ModelMap model, @PathVariable String user_name,@RequestParam(required = false) String request_type) {
        UserModule param=new UserModule();
        param.setUser_name(user_name);
        try {
            UserModule userModule=service.selectUserModuleByName(param);
            if(userModule==null){
                model.addAttribute("message","对不起，你没有访问权限,请先注册并且配置启用此模块后即可免费试用。");
                return new ModelAndView("error-400");
            }else{
                model.addAttribute("userModule",userModule);
                if(request_type==null){
                    return new ModelAndView("business/index");
                }else{
                    switch (request_type) {
                        case "2":
                            return new ModelAndView("business/page_pricing");
                        case "3":
                            return new ModelAndView("business/page_about");
                        case "4":
                            return new ModelAndView("business/page_contact");
                        case "5":
                            return new ModelAndView("business/app_login");
//                        case "6":
//                            return new ModelAndView("business/page_edit");
//                        case "7":
//                            return new ModelAndView("business/form_step");
//                        case "8":
//                            return new ModelAndView("business/visual_edit");
                        default:
                            return new ModelAndView("business/index");
                    }
                }
            }

        } catch (Exception e) {
            model.addAttribute("message","对不起，你没有访问权限,请先注册并且配置启用此模块后即可免费试用。");
            return new ModelAndView("error-400");
        }
    }
    /**
     * 登录安全性检测
     * @param param
     * @param request
     * @return
     */
    @PostMapping(value = "/login.business")
    public @ResponseBody
    Map<String, Object> login(@RequestBody UserModule param, HttpServletRequest request){
        Map<String,Object> returnMap=new HashMap<String,Object>();
        UserModule userModule=null;
        if(param!=null) {
            try {
                userModule=service.selectUserModuleByName(param);
            }catch (Exception e){
                returnMap.put("message","登录失败，请联系管理员");
                return returnMap;
            }

            if(userModule!=null){
                returnMap.put("message","");
                //在session中保存用户身份信息
                request.getSession().setAttribute("user_session",userModule);
            }else{
                returnMap.put("message","登录失败：用户名或密码错误");
            }
        }
        return returnMap;
    }
    /**
     * 修改配置数据
     * @param param
     * @return
     */
    @PostMapping(value="/editJsonValue.business")
    public @ResponseBody
    Map<String, Object> editJsonValue(@RequestBody UserModule param){
        Map<String,Object> returnMap=new HashMap<String,Object>();
        int updateStatus=0;
        if(param!=null) {
            try {
                updateStatus=service.updateUserModuleByName(param);
            }catch (Exception e){
                returnMap.put("message","修改失败，请联系管理员,失败原因："+e.getMessage());
                return returnMap;
            }

            if(updateStatus==1){
                returnMap.put("message","");
            }else{
                returnMap.put("message","修改失败：请联系管理员");
            }
        }
        return returnMap;
    }

    /**
     * 保存图片上传记录
     * @param param
     * @return
     */
    @PostMapping(value="/saveUserPicture.business")
    public @ResponseBody
    Map<String, Object> saveUserPictureByName(@RequestBody UserPicture param){
        Map<String,Object> returnMap=new HashMap<String,Object>();
        int status=0;
        if(param!=null) {
            try {
                status=picservice.saveUserPicture(param);
            }catch (Exception e){
                returnMap.put("message","添加失败，请联系管理员,失败原因："+e.getMessage());
                return returnMap;
            }

            if(status==1){
                returnMap.put("message","");
            }else{
                returnMap.put("message","添加失败：请联系管理员");
            }
        }
        return returnMap;
    }

    /**
     * 保存留言信息
     * @param param
     * @return
     */
    @PostMapping(value="/saveUserMessage.business")
    public @ResponseBody
    Map<String, Object> saveUserMessage(@RequestBody UserMessage param){
        Map<String,Object> returnMap=new HashMap<String,Object>();
        int status=0;
        if(param!=null) {
            try {
                param.setCreate_time(new Date());
                param.setStatus("0");
                status=userMessageService.saveUserMessage(param);
            }catch (Exception e){
                returnMap.put("message","留言失败，请联系管理员,失败原因："+e.getMessage());
                return returnMap;
            }

            if(status==1){
                returnMap.put("message","");
            }else{
                returnMap.put("message","留言失败：请联系管理员");
            }
        }
        return returnMap;
    }

    /**
     * 查询留言列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/user_msg_view.business",method =RequestMethod.POST)
	@ResponseBody
	Map<String, Object> getUserList(@RequestBody UserMessage param) {
		try {
		    param.setCurrentPage(param.getCurrentPage()+1);
			setPageParam(param);
			List<UserMessage> list=userMessageService.selectUserMessageList(param);
			Long count=(long)userMessageService.selectUserMessageListCount(param).size();
			Page<UserMessage> page =getPageInfo(param,list,count);
			return ResUtil.Res(page);
		} catch (ServiceException se) {
			//log.warn("查询用户视图:"+se.getMessage());
			return ResUtil.Res(se);
		} catch (Exception e) {
			//log.error("查询用户视图:", e);
			return ResUtil.Res(e);
		}
	}
}
